<?php
require "././vendor/autoload.php";

use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exchange\AMQPExchangeType;
use PhpAmqpLib\Wire\AMQPTable;

$exchangeName = "ttlExchange";
$queueName = "ttlQueueName";
$routeKey = "ttlRouteKey";


$host = PHP_OS == "Linux" ? "127.0.0.1" : "10.0.0.200";
$conn = new AMQPStreamConnection($host, '5672', 'wyqgg', '123456');

$ch = $conn->channel();

$ch->confirm_select();

$ch->set_nack_handler(function (AMQPMessage $message) {
    echo "消息传递失败!" . $message->getBody() . PHP_EOL;
});

$ch->set_ack_handler(function (AMQPMessage $message) {
    echo "消息传递成功!" . $message->getBody() . PHP_EOL;
});


$ch->exchange_declare($exchangeName, AMQPExchangeType::DIRECT, false, true, false, false, false);
$queueArguments = [
//    'x-message-ttl' => 10 * 1000,
];
$arguments = new AMQPTable($queueArguments);
$ch->queue_declare($queueName, false, true, false, false, false, $arguments);
$ch->exchange_declare($exchangeName, AMQPExchangeType::DIRECT, false, true, false);

$ch->set_return_listener(function ($code, $text, $queueName, $exchangeName, $routeKey, AMQPMessage $message) {
    echo $text . PHP_EOL;
});

$ch->queue_bind($queueName, $exchangeName, $routeKey);
$properties = ['expiration' => 10*1000 ];
$message = new AMQPMessage('ttl', $properties);

$ch->basic_publish($message, $exchangeName, $routeKey, true);

$ch->wait_for_pending_acks_returns();

$ch->close();
$conn->close();